package com.xuedongzhilian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.RandomUtil;
import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.UserInfoExtendDTO;
import com.xuedongzhilian.dto.UserDTO;
import com.xuedongzhilian.entity.User;
import com.xuedongzhilian.mapper.UserMapper;
import com.xuedongzhilian.service.IUserManagementService;
import com.xuedongzhilian.service.IUserInfoExtendService;
import com.xuedongzhilian.service.IUserService;
import com.xuedongzhilian.utils.SystemConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.xuedongzhilian.utils.RedisConstants.LOGIN_USER_KEY;
import static com.xuedongzhilian.utils.RedisConstants.LOGIN_USER_TTL;
import static com.xuedongzhilian.utils.SystemConstants.USER_NICK_NAME_PREFIX;

/**
 * <p>
 * 用户管理服务实现类（学动智联系统）
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Service
public class UserManagementServiceImpl implements IUserManagementService {

    @Resource
    private IUserInfoExtendService userInfoExtendService;
    
    @Resource
    private IUserService userService;
    
    @Resource
    private UserMapper userMapper;
    
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result wechatLogin(String code, HttpSession session) {
        // TODO 实现微信快捷登录功能
        // 这里只是一个示例实现，实际需要调用微信API获取用户信息
        
        // 模拟微信登录成功，创建或获取用户
        User user = createOrGetWechatUser(code);
        
        // 生成token并保存用户信息到Redis
        String token = UUID.randomUUID().toString();
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        Map<String, Object> userMap = BeanUtil.beanToMap(userDTO, new HashMap<>(),
                CopyOptions.create()
                        .setIgnoreNullValue(true)
                        .setFieldValueEditor((fieldName, fieldValue) -> fieldValue.toString()));
        
        // 存储到Redis
        String tokenKey = LOGIN_USER_KEY + token;
        stringRedisTemplate.opsForHash().putAll(tokenKey, userMap);
        // 设置token有效期
        stringRedisTemplate.expire(tokenKey, LOGIN_USER_TTL, TimeUnit.MINUTES);
        
        // 返回token
        Map<String, String> result = new HashMap<>();
        result.put("token", token);
        return Result.ok(result);
    }

    private User createOrGetWechatUser(String code) {
        // 模拟根据微信code获取用户信息
        // 实际项目中需要调用微信API获取openid等信息
        
        // 这里简化处理，直接创建一个用户
        User user = new User();
        user.setId(1L); // 模拟用户ID
        user.setNickName(USER_NICK_NAME_PREFIX + RandomUtil.randomString(10));
        return user;
    }

    @Override
    public UserInfoExtendDTO getUserInfoExtend(Long userId) {
        return userInfoExtendService.getUserInfoExtend(userId);
    }

    @Override
    public Result updateUserInfoExtend(Long userId, UserInfoExtendDTO userInfoExtendDTO) {
        // TODO 更新用户扩展信息
        return Result.fail("功能未完成");
    }
}